package com.eecs393.drymouth;

import com.eecs393.drymouth.R;

import android.app.Activity;
import android.app.AlertDialog;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.*;
import android.content.DialogInterface;
import android.content.Intent;

public class ConditionSearch extends Activity implements OnClickListener
{
	private EditText symptomSet;
	private boolean login_status = false;
	private boolean justLoggedIn = false;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        symptomSet = (EditText) this.findViewById(R.id.EditText_symptoms);
        Button button = (Button) this.findViewById(R.id.Button_submitsearch);
        Button loginButton = (Button) this.findViewById(R.id.Button_login);
        button.setOnClickListener(this);
        loginButton.setOnClickListener(new loginListener(this));
        
        Bundle extras = this.getIntent().getExtras();
        if(extras != null)
        {
        	login_status = extras.getBoolean("login_status");
        }
        if(login_status)
        {
        	this.findViewById(R.id.Button_login).setVisibility(View.INVISIBLE);
        	
        	justLoggedIn = extras.getBoolean("newLoginKey");
        	
        	if (justLoggedIn){      		
        		AlertDialog.Builder adb = new AlertDialog.Builder(this);
        		adb.setTitle("Login Successful!");    		
			
        		String body = "Welcome " + extras.getString("username") + "!";
			
        		adb.setMessage(body);
        		adb.setCancelable(true);
        		adb.setNeutralButton("OK", new DialogInterface.OnClickListener(){
        			@Override
        			public void onClick(DialogInterface dialog, int which){
        				dialog.dismiss();
        			}
        		});
        		AlertDialog ad = adb.create();
        		ad.show();
        	}
        }
    }
    
    public void onClick(View v)
    {
    	assert symptomSet != null;
	    String symptoms = symptomSet.getText().toString();
	    symptoms = symptoms.replace(',', '~');
	    symptoms = symptoms.replace(' ', '_');
	    Intent submitSymptoms = new Intent(this, ConditionResults.class);
	    submitSymptoms.putExtra("symptomkey", symptoms);
	    submitSymptoms.putExtra("loginstatuskey", login_status);
	    this.startActivity(submitSymptoms);
    }
    
    private class loginListener implements OnClickListener
    {
    	Activity myActivity;
    	public loginListener(Activity a)
    	{
    		myActivity = a;
    	}
    	
		@Override
		public void onClick(View v) {
			Intent login = new Intent(myActivity, EnterLogin.class);
    		myActivity.startActivity(login);			
		}
    	
    }
}
